package com.justech.eat.service.event.listener;

import com.justech.eat.model.SettlementResult;
import com.justech.eat.repository.OrderRepository;
import com.justech.eat.service.OrderDetailAbnormalService;
import com.justech.eat.service.OrderDetailService;
import com.justech.eat.service.OrderService;
import com.justech.eat.service.component.OrderDetailAfterSettlementProcessor;
import com.justech.eat.service.event.OrderDetailFinalEvent;
import com.justech.eat.service.impl.OrderBookSubmit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: eat-service
 * @description: 报餐结果监听器
 * @author: feifei.li
 * @create: 2020-06-08 19:40
 **/
@Component
public class OrderDetailResultListener implements ApplicationListener<OrderDetailFinalEvent> {

    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    OrderDetailAbnormalService orderDetailAbnormalService;
    @Autowired
    OrderBookSubmit orderBookSubmit;

    @Qualifier("overlordMealProcessor")
    @Autowired
    OrderDetailAfterSettlementProcessor overlordMealProcessor;

    @Qualifier("standSbUpProcessor")
    @Autowired
    OrderDetailAfterSettlementProcessor standSbUpProcessor;

    @Override
    public void onApplicationEvent(OrderDetailFinalEvent event) {
        List<SettlementResult> settlementResults = event.getSettlementResults();
        for (SettlementResult settlementResult : settlementResults) {
            if (settlementResult.getResult() == 3) {
                try {
                    overlordMealProcessor.afterSettlement(settlementResult);
                } catch (RuntimeException e) {
                    System.out.println(e.getMessage());
                }
            } else if (settlementResult.getResult() == 2) {
                standSbUpProcessor.afterSettlement(settlementResult);
            } else {
                pass(settlementResult);
            }
        }
    }

    private void pass(SettlementResult sr) {
        orderDetailService.finalOrderDetail(sr.getDate(), sr.getJobNumber(), 1);
    }
}
